package com.woniuxy.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author lg
 * @time 2020/07/05 15:35:00
 */
@Controller
@RequestMapping("auth")

public class AuthController {
   @RequestMapping("login")
    public String login(String username,String password){
      Subject subject = SecurityUtils.getSubject ();
      UsernamePasswordToken usernamePasswordToken=new UsernamePasswordToken (username,password);
      String path="";

      try {
         subject.login (usernamePasswordToken);
         path="home";
      } catch (AuthenticationException e) {
         e.printStackTrace ();
         path="index";
      }
       /* redirect 重定向*/
       return "redirect:/"+path+".jsp";

   }


/*   @RequestMapping("logout")
   public String logout(){
        Subject subject=SecurityUtils.getSubject ();
        subject.logout ();
        return "redirect:/index.jsp";
   }*/

    @RequiresAuthentication
    @RequiresRoles ("admin")
    @RequestMapping("f1")
    public String f1(){
        System.out.println ("AuthController.f1");
        return  null;

    }

    @RequiresAuthentication
    @RequiresRoles ("guest")
    @RequestMapping("f2")
    public String f2(){
        System.out.println ("AuthController.f2");
        return  null;
    }
}
